package com.example.system_backend.service.impl;


import com.example.system_backend.entity.User;
import com.example.system_backend.repository.UserRepository;
import com.example.system_backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public Integer login(String email, String password) {
        User user = userRepository.findByEmail(email);
        if (user == null) {
            return -1;
        }
        user = userRepository.findByEmailAndPassword(email, password);
        if (user == null) {
            return -2;
        } else {
            return user.getType();
        }
    }

    @Override
    public Integer register(String email, String name, String password) {
        User user = userRepository.findByEmail(email);
        if (user != null) {
            return -1;
        }
        user = new User(email, name, password, 1);
        userRepository.save(user);
        return 0;
    }
}
